package com.chexiehui.jc.mvp.forum;

import com.alibaba.sdk.android.oss.ClientException;
import com.alibaba.sdk.android.oss.OSS;
import com.alibaba.sdk.android.oss.ServiceException;
import com.alibaba.sdk.android.oss.callback.OSSCompletedCallback;
import com.alibaba.sdk.android.oss.internal.OSSAsyncTask;
import com.alibaba.sdk.android.oss.model.PutObjectRequest;
import com.alibaba.sdk.android.oss.model.PutObjectResult;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.StringUtils;
import com.chexiehui.jc.base.ApiCallback;
import com.chexiehui.jc.base.BaseModel;
import com.chexiehui.jc.base.BasePresenter;
import com.chexiehui.jc.utils.AliossUtil;

import io.reactivex.Observable;
import io.reactivex.ObservableOnSubscribe;

/**
 * @Description 我的爱车添加
 * @Auth SongJun
 * @Date 2020/05/21 00:18
 **/
public class ForumDynamicPostPresenter extends BasePresenter<ForumDynamicPostCovenant.View, ForumDynamicPostCovenant.Stores>
        implements ForumDynamicPostCovenant.Presenter {

    public ForumDynamicPostPresenter(ForumDynamicPostCovenant.View view) {
        attachView(view);
    }

    @Override
    public void uploadImage(final String filePath) {
        if (StringUtils.isTrimEmpty(filePath)) {
            mvpView.showToast("请选择图片");
            return;
        }
        mvpView.showLoading("正在上传");
        addSubscription(Observable.create((ObservableOnSubscribe<BaseModel<String>>) emitter -> {
            try {
                OSS ossClient = AliossUtil.createOssClient();
                final String[] tempArr = {""};
                String mObjectKeyPath = AliossUtil.createPostImagePath(filePath);
                // 异步构造上传请求  核心代码 就下面几行  再走阿里,上传给阿里
                PutObjectRequest objectRequest = new PutObjectRequest(AliossUtil.BUCKET_NAME, mObjectKeyPath, filePath);
                OSSAsyncTask<PutObjectResult> task = ossClient.asyncPutObject(objectRequest, new OSSCompletedCallback<PutObjectRequest, PutObjectResult>() {
                    @Override
                    public void onSuccess(PutObjectRequest request, PutObjectResult result) {
                        LogUtils.e("Image---onSuccess", request.getObjectKey());
                        LogUtils.e("Image---onSuccess1", result.getServerCallbackReturnBody());
                        tempArr[0] = "/" + request.getObjectKey();
                    }

                    @Override
                    public void onFailure(PutObjectRequest request, ClientException clientException, ServiceException serviceException) {
                        LogUtils.e("Image---onFailure", request.getObjectKey());
                        //videoUrl[0] = videoUrl[0] = "/" + request.getObjectKey();
                        if (emitter != null && !emitter.isDisposed()) {
                            emitter.onError(new Throwable("行驶证上传失败"));
                        }
                    }
                });
                task.waitUntilFinished();
                task.cancel();
                emitter.onNext(new BaseModel<>(0, "行驶证上传成功", tempArr[0]));
            } catch (Exception e) {
                e.printStackTrace();
                emitter.onError(e);
            }
        }), new ApiCallback<BaseModel<String>>(mvpView) {
            @Override
            public void onSuccess(BaseModel<String> model) {
                mvpView.hide();
                mvpView.onUploadImageSuccess(model);
            }

            @Override
            public void onFailure(int code, String msg) {
                mvpView.hide();
                mvpView.onUploadImageFailure(new BaseModel<>(code, msg));
            }
        });
    }

    @Override
    public void addPost() {
        if (StringUtils.isTrimEmpty(mvpView.getPostTitle()) || mvpView.getPostTitle().length() < 6) {
            mvpView.showToast("请输入6-30字标题");
            return;
        }
        String postContent = mvpView.getPostContent();
        if (StringUtils.isTrimEmpty(postContent)) {
            mvpView.showToast("请输入内容");
            return;
        }
        if (StringUtils.isTrimEmpty(mvpView.getPostForum())) {
            mvpView.showToast("请选择论坛");
            return;
        }
        mvpView.showLoading("正在保存");
        addSubscription(appStores.addPost(mvpView.getPostForum(), mvpView.getPostTitle(),
                mvpView.getPostContent(), mvpView.getPostLat(),mvpView.getPostLng(),
                mvpView.getPostCity(), mvpView.getPostPhotos(), "0"),
                new ApiCallback<BaseModel<Object>>(mvpView) {
                    @Override
                    public void onSuccess(BaseModel<Object> model) {
                        if (model.getResultCode() == 0) {
                            mvpView.hide();
                            mvpView.onAddPostSuccess(model);
                        } else {
                            onFailure(model.getResultCode(), model.getResultInfo());
                        }
                    }

                    @Override
                    public void onFailure(int code, String msg) {
                        mvpView.hide();
                        mvpView.onAddPostFailure(new BaseModel<>(code, msg));
                    }
                });
    }

}